package team.you.ticket.service.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.you.ticket.dao.CarMapper;
import team.you.ticket.dao.TrainNumMapper;
import team.you.ticket.pojo.Car;
import team.you.ticket.pojo.Result;
import team.you.ticket.pojo.TrainName;
import team.you.ticket.pojo.TrainNum;
import team.you.ticket.service.TrainNumService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class TrainNumServiceImpl implements TrainNumService
{
    @Autowired
    private TrainNumMapper trainNumMapper;
    @Autowired
    private CarMapper carMapper;
    @Override
    public Result getAllTrainNums(Integer page, Integer limit) {
        Result result = new Result();
        if (page <= 0) {
            page = 1;
            page = (page - 1) * limit;
        } else page = (page - 1) * limit;
        List<TrainNum> trainNums = trainNumMapper.selectAll(page, limit);
        Integer count = trainNumMapper.getCount();
        result.setData(trainNums);
        result.setCount(count);
        return result;
    }

    @Override
    public Result add(TrainNum trainNum) {

        Result result=new Result();
        String uuid= UUID.randomUUID().toString();
        try {
//            TrainNum trainNum1=new TrainNum(uuid,trainNum.getCarId(),trainNum.getStarTime(),trainNum.getStarSite(),trainNum.getEndTime(),trainNum.getEndSite(),trainNum.getRemarks());
            trainNum.setId(uuid);
            Car car=carMapper.selectByPrimaryKey(trainNum.getCarId());
            trainNum.setPopleNum(car.getNumber());
            Integer i=trainNumMapper.insert(trainNum);
            if (i==1)
            {
                result.setMsg("新增成功");
                return result;
            }
            else
            {
                result.setCode(500);
                result.setMsg("新增失败");
                return result;
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("系统繁忙");
            return result;
        }
    }

    @Override
    public Result getCarId() {
        Result result=new Result();
        List<TrainName> carId=carMapper.selectAllCarNum();
        if (carId!=null)
        {
            result.setMsg("获取成功");
            result.setData(carId);
            return result;
        }
        else
        {
            result.setCode(500);
            result.setMsg("获取失败");
            return result;
        }
    }

    @Override
    public Result del(String id) {
        Result result=new Result();
        try {
            Integer i=trainNumMapper.deleteByPrimaryKey(id);
            if (i==1)
            {
                result.setMsg("删除成功");
                return result;
            }
            else
            {
                result.setMsg("删除失败");
                result.setCode(500);
                return result;
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("系统繁忙");
            return result;
        }
    }

    @Override
    @Transactional //添加事务
    public Result delete(String[] ids) {
        Result result = new Result();
        try {
            for (int i = 0; i < ids.length; i++) {
                trainNumMapper.deleteByPrimaryKey(ids[i]);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("系统繁忙");
            return result;
        }
    }

    @Override
    public Result update(String id, String value, String field) {
        Result result=new Result();
        try {
            Integer i=trainNumMapper.updateByKey(id,value,field);
            if (i==1)
            {
                result.setMsg("更新成功");
                return result;
            }
            else
            {
                result.setCode(500);
                result.setMsg("更新失败");
                return result;
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("系统繁忙");
            return result;
        }
    }

    @Override
    public Result getAll(String starSite, String endSite, Date time,Integer page, Integer limit) {
        Result result=new Result();
        if (page <= 0) {
            page = 1;
            page = (page - 1) * limit;
        } else page = (page - 1) * limit;
        List<TrainNum> trainNums = trainNumMapper.selectAll1(page, limit,starSite,endSite);
        try {
            for (int i=0;i<trainNums.size();)
            {
                if(!TimeMatch(trainNums.get(i).getStarTime(),time))
                {
                    trainNums.remove(i);
                }
                else i++;
            }
            Integer count = trainNumMapper.getCount1(starSite,endSite);
            result.setData(trainNums);
            result.setCount(count);
            return result;
        }catch (Exception e)
        {
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("系统繁忙");
            return result;
        }
    }

    @Override
    public Result selectByLike(String value, String type, Integer page, Integer limit) {
        Result result = new Result();
        if (page <= 0) {
            page = 1;
            page = (page - 1) * limit;
        } else page = (page - 1) * limit;
        Integer type0=Integer.parseInt(type);
        List<TrainNum> trainNums = trainNumMapper.selectByLike(value,type0,page, limit);
        List<TrainNum> count = trainNumMapper.selectByLike(value,type0,null,null);
        result.setData(trainNums);
        result.setCount(count.size());
        return result;
    }

    @Override
    public Result selectByTime(String time, Integer page, Integer limit) {
        Result result=new Result();
        String beginTime="";
        String endTime="";
        if(time!=null && time!="")
        {
            String[] split=time.split("~");
            beginTime=split[0];
            endTime=split[1];
        }
        List<TrainNum> trainNums=trainNumMapper.selectByTime(beginTime,endTime);
        result.setData(trainNums);
        result.setCount(trainNums.size());
        return result;
    }

    //时间比较
    private boolean TimeMatch(Date time,Date date)
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str1=sdf.format(time).substring(0,10).replace("-","");//将Data转为String，在截取日期，只取数字
        String str2=sdf.format(date).substring(0,10).replace("-","");
        int int1=Integer.parseInt(str1);//转为int比较
        int int2=Integer.parseInt(str2);
        if (int1==int2)
        {
            return true;//在同一天返回true
        }
        else return false;//否则返回false
    }
}
